<template>
  <transition name="slide">
    <div class="user-center">
      <div class="back" @click="back">
        <i class="icon-back"></i>
      </div>
      <div class="switches-wrapper">
       <switches @active='switchActive' :items='switchItems' :currentIndex='switchIndex'></switches>
      </div>
      <div  class="play-btn" @click="randomPlayAll">
        <i class="icon-play"></i>
        <span class="text" >随机播放全部</span>
      </div>
      <div class="list-wrapper">
        <scroll  class="list-scroll" :data='favoriteSong' v-if="switchIndex===0">
          <div class="list-inner">
            <song-list :songs='favoriteSong' @selectSong='selectSong'></song-list>
          </div>
        </scroll>
        <scroll :data='playHistory' class="list-scroll"  v-if="switchIndex===1">
          <div class="list-inner">
            <song-list :songs='playHistory' @selectSong='selectSong'></song-list>
          </div>
        </scroll>
      </div>
      <div class="no-result-wrapper" v-show='noResultShow'>
        <no-result :title="noResultTitle"></no-result>
      </div>
    </div>
  </transition>
</template>

<script>
import Scroll from 'base/scroll/scroll'
import Switches from 'base/switches/switches'
import SongList from 'base/song-list/song-list'
import { mapGetters, mapActions } from 'vuex'
import Song from 'common/js/song'
import NoResult from 'base/no-result/no-result'

export default {
  data() {
    return {
      switchIndex: 0,
      switchItems: [
        {name:'我喜欢的'},{name:'最近在听'}
      ]
    }
  },
  methods: {
    back() {
      this.$router.back()
    },
    switchActive(index) {
      this.switchIndex=index
      // console.log(this.playHistory)
    },
    selectSong(song) {
      this.insertSong(song)
    },
    randomPlayAll() {
      let list= this.switchIndex===0 ? this.favoriteSong : this.playHistory
      // 无记录 
      if(list.length===0) {
        return
      }
      list=list.map((song)=> {
        return new Song(song)
      })
      this.randomPlay({list})
    },
    ...mapActions([
      'insertSong',
      'randomPlay'
    ])
  },
  computed: {
    ...mapGetters([
      'playHistory',
      'favoriteSong'
    ]),
    noResultShow() {
      if(this.switchIndex===0) {
        return !this.favoriteSong.length
      }else {
        return !this.playHistory.length
      }
    },
    noResultTitle() {
      if(this.switchIndex===0) {
        return '还没有收藏过歌曲'
      }else {
        return '还没有听过歌'
      }
    }
  },
  components: {
    Scroll,
    Switches,
    SongList,
    NoResult
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .user-center
    position: fixed
    top: 0
    bottom: 0
    z-index: 100
    width: 100%
    background: $color-background
    &.slide-enter-active, &.slide-leave-active
      transition: all 0.3s
    &.slide-enter, &.slide-leave-to
      transform: translate3d(100%, 0, 0)
    .back
      position absolute
      top: 0
      left: 6px
      z-index: 50
      .icon-back
        display: block
        padding: 10px
        font-size: $font-size-large-x
        color: $color-theme
    .switches-wrapper
      margin: 10px 0 30px 0
    .play-btn
      box-sizing: border-box
      width: 135px
      padding: 7px 0
      margin: 0 auto
      text-align: center
      border: 1px solid  $color-text-l
      color: $color-text-l
      border-radius: 100px
      font-size: 0
      .icon-play
        display: inline-block
        vertical-align: middle
        margin-right: 6px
        font-size: $font-size-medium-x
      .text
        display: inline-block
        vertical-align: middle
        font-size: $font-size-small
    .list-wrapper
      position: absolute
      top: 110px
      bottom: 0
      width: 100%
      .list-scroll
        height: 100%
        overflow: hidden
        .list-inner
          padding: 20px 30px
    .no-result-wrapper
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>